﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.AspNet.FriendlyUrls;

namespace BabyShop.CP
{
    public partial class Orders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
            lblMsg.Text = "";
        }

        public void FillGrid()
        {
            DataTable dt = DAO.Order.getAll();
            GridView.DataSource = dt;
            GridView.DataBind();
        }

        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView.PageIndex = e.NewPageIndex;
            GridView.DataBind();
        }

        protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Detail"))
            {
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                int index = gvr.RowIndex;
                Label lbID = (Label)GridView.Rows[index].FindControl("lbID");
                Response.Redirect("odetail.aspx?id=" + lbID.Text);
            }
        }

        protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(GridView.DataKeys[e.RowIndex].Values[0].ToString());
            DAO.Order.Delete(id);
            FillGrid();
            lblMsg.Text = "Đã xóa đơn hàng id:" + id.ToString();
        }
       
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string fullname = txtFullname.Text;            
            double total = 0;
            if (!string.IsNullOrWhiteSpace(txtTotal.Text)) 
            {
                total = double.Parse(txtTotal.Text);
            }
            int order_status = int.Parse(completed.SelectedValue);                
            DataTable dt = DAO.Order.find(fullname,total,order_status);
            if (dt.Rows.Count > 0)
            {
                GridView.DataSource = dt;
                GridView.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                GridView.DataSource = dt;
                GridView.DataBind();

                int TotalColumns = GridView.Rows[0].Cells.Count;
                GridView.Rows[0].Cells.Clear();
                GridView.Rows[0].Cells.Add(new TableCell());
                GridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                GridView.Rows[0].Cells[0].Text = "Không có dữ liệu";
            }
        }
    }
}
